package com.zxg.zbc.biz.util;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.commons.lang.StringUtils;

public class MailUtils {
  private static final String ALIDM_SMTP_HOST = "smtp.exmail.qq.com";
  private static final int    ALIDM_SMTP_PORT = 465;

  static String               d_email         = "notice@zhixiangge.com", d_password = "Zhixiangge123", d_host = ALIDM_SMTP_HOST, d_port = "465";

  public static void main(String[] args) throws Exception {
    sendMail("Test_系统提醒[有人提交采购需求啦，请尽快处理]", "用户［zhangsan］联系方式［17788889999］<br> 诉求［啊短发看风景啊空间发］", "ziyi@zhixiangge.com", "hr@zhixiangge.com",
        "xunyu@zhixiangge.com");
  }

  /**
   * 发邮件
   * 
   * @param title
   * @param content
   * @param toMail
   *          多个收件人请用","分隔
   * @throws Exception
   */
  public static void sendMail(String title, String content, String toMail, String ccMail, String bccMail) throws Exception {
    StringBuilder html = new StringBuilder();
    html.append(content);
    html.append("<br>");
    html.append("<br>");
    html.append("----------------------------------------------------");
    html.append("<br>");
    html.append("系统邮件，请勿回复！");

    // html="您好hangding,很久不见了";

    // 配置发送邮件的环境属性
    final Properties props = new Properties();
    // 表示SMTP发送邮件，需要进行身份验证
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.host", ALIDM_SMTP_HOST);
    // props.put("mail.smtp.port", ALIDM_SMTP_PORT);
    // // 如果使用ssl，则去掉使用25端口的配置，进行如下配置,
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.auth", "true"); // If you need to authenticate
    // Use the following if you need SSL
    props.put("mail.smtp.socketFactory.port", ALIDM_SMTP_PORT);
//    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.fallback", "false");

    // 发件人的账号
    props.put("mail.user", d_email);
    // 访问SMTP服务时需要提供的密码
    props.put("mail.password", d_password);

    // 构建授权信息，用于进行SMTP进行身份验证
    Authenticator authenticator = new Authenticator() {
      @Override
      protected PasswordAuthentication getPasswordAuthentication() {
        // 用户名、密码
        String userName = props.getProperty("mail.user");
        String password = props.getProperty("mail.password");
        return new PasswordAuthentication(userName, password);
      }
    };
    // 使用环境属性和授权信息，创建邮件会话
    Session mailSession = Session.getInstance(props, authenticator);

    // 创建邮件消息
    MimeMessage message = new MimeMessage(mailSession);
    // 设置发件人
    InternetAddress form = new InternetAddress(props.getProperty("mail.user"));
    message.setFrom(form);

    // 设置收件人
    String[] to = toMail.split(",");
    InternetAddress[] sendTo = new InternetAddress[to.length];
    for (int i = 0; i < to.length; i++) {
      sendTo[i] = new InternetAddress(to[i]);
    }
    message.setRecipients(MimeMessage.RecipientType.TO, sendTo);

    // 设置抄送人
    if (StringUtils.isNotEmpty(ccMail)) {
      String[] ccTo = ccMail.split(",");
      InternetAddress[] sendCcTo = new InternetAddress[ccTo.length];
      for (int i = 0; i < ccTo.length; i++) {
        sendCcTo[i] = new InternetAddress(ccTo[i]);
      }
      message.setRecipients(MimeMessage.RecipientType.CC, sendCcTo);
    }

    // 设置密送人
    if (StringUtils.isNotEmpty(bccMail)) {
      String[] bccTo = bccMail.split(",");
      InternetAddress[] sendBccTo = new InternetAddress[bccTo.length];
      for (int i = 0; i < bccTo.length; i++) {
        sendBccTo[i] = new InternetAddress(bccTo[i]);
      }
      message.setRecipients(MimeMessage.RecipientType.BCC, sendBccTo);
    }

    String nick = javax.mail.internet.MimeUtility.encodeText("找包材网");
    message.setFrom(new InternetAddress(nick + " <" + d_email + ">"));
    // 设置邮件标题
    message.setSubject(title);
    // 设置邮件的内容体
    message.setContent(html.toString(), "text/html;charset=UTF-8");

    // 发送邮件
    Transport.send(message);
    System.out.println("system mail: " + html.toString());
  }
}